﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.DiaoDongService;
using CommunitySystem.Views;
using System.Collections.ObjectModel;


namespace CommunitySystem.PageView
{
    public partial class ZREMPControl : AsyncCtrl
	{

        DiaoDongServiceClient client = new DiaoDongServiceClient();
        private ZJZZRModel _ZJZ = new ZJZZRModel();
		public ZREMPControl(ZJZZRModel ZJZ)
		{
			// 为初始化变量所必需
			InitializeComponent();
            _ZJZ = ZJZ;
            IsWorking.Visibility = Visibility.Visible;
            busy.BusyContent = "正在同步数据，请稍后...";

            client.GetZJZZR_EmpCompleted += new EventHandler<GetZJZZR_EmpCompletedEventArgs>(client_GetZJZZR_EmpCompleted);
            client.GetZJZZR_EmpAsync(string.Format(" and ZJZZRId={0}",ZJZ.Id),0,0);
		}

        void client_GetZJZZR_EmpCompleted(object sender, GetZJZZR_EmpCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
            IsWorking.Visibility = Visibility.Collapsed;
        }
        /// <summary>
        /// 人员转任  上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnZR_Click(object sender, RoutedEventArgs e)
        {
            //if (this.dg.SelectedItem == null)
            //{
            //    SilverlightMessageBoxLibrary.Message.InfoMessage("请选择一条信息进行人员转任!");
            //    return;
            //}
            //            if (this.DGRole.ItemsSource != null)
            if (this.dg.ItemsSource != null)
            {
                foreach (ZJZZR_Emp i in (ObservableCollection<ZJZZR_Emp>)dg.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0 && selectedempmodel.Count==1)
            {

                var sel = selectedempmodel.FirstOrDefault();
                if (sel.BZ == "已转任")
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("该人员已转任，不可再操作!");
                    return;
                }
                EMPSBWindow win = new EMPSBWindow(sel);
                win.Show();
                win.Closed += new EventHandler(win_Closed);
            }
             else
                 SilverlightMessageBoxLibrary.Message.InfoMessage("请选择一条信息进行人员转任!");
        }

        void win_Closed(object sender, EventArgs e)
        {
            client.GetZJZZR_EmpAsync(string.Format(" and ZJZZRId={0}", _ZJZ.Id), 0, 0);
        }
        /// <summary>
        /// 确认转任
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            if (_ZJZ.BZ == "人员已转任")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("整建制转入人员已全部转入，不可再操作！");
                return;
            }
            if (this.dg.ItemsSource != null)
            {
                foreach (ZJZZR_Emp i in (ObservableCollection<ZJZZR_Emp>)dg.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                string ids = "";
                foreach (var item in selectedempmodel)
                {
                    if (item.BZ == "未转任")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.XM + "还未转任，请转任后进行确认！");
                        return;
                    }
                    ids += item.Id + ",";
                }
            }
            if (MessageBoxResult.OK == MessageBox.Show("确定转任吗?", "提示", MessageBoxButton.OKCancel))
            {
              
                    client.QRZRCompleted += new EventHandler<QRZRCompletedEventArgs>(client_QRZRCompleted);
                    client.QRZRAsync(selectedempmodel.FirstOrDefault().ZJZZRId.ToString());
             
            }
        }

        void client_QRZRCompleted(object sender, QRZRCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("操作成功！");
            else SilverlightMessageBoxLibrary.Message.InfoMessage("操作失败！");

            client.GetZJZZR_EmpAsync(string.Format(" and ZJZZRId={0}", _ZJZ.Id), 0, 0);
        }
        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="sender"></param>ii
        /// <param name="e"></param>
        private ObservableCollection<ZJZZR_Emp> selectedempmodel = new ObservableCollection<ZJZZR_Emp>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZJZZR_Emp p in this.dg.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {

                    if (selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault());
                    }
                }
            }
        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
        }
	}
}